//
//  LMLoginController.m
//  EPA
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMLoginController.h"
#import "UserInfoManager.h"
#import "CK_MenuViewController.h"
#import "LMHomePageViewController.h"

#import "LMCollectController.h"
@interface LMLoginController ()
@property (weak, nonatomic) IBOutlet UITextField *EmailTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LMLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)didClickyoukeEnterAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)didClickLoginAction:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/login"];
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    
    [mRequest setHTTPMethod:@"POST"];
    
    self.EmailTextField.text = @"597562703@qq.com";
    self.passwordTextField.text = @"123456";
    NSString *bodyStr = [NSString stringWithFormat:@"email=%@&passwd=%@",self.EmailTextField.text,self.passwordTextField.text];
    
    NSData *data = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [mRequest setHTTPBody:data];
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession]dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data) {
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            NSMutableDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDic = jsonDict[@"data"];
            NSString *msg = jsonDict[@"data"][@"msg"] ;
            msg = msg ? msg : @"登录成功";
            NSInteger result = [jsonDict[@"result"] integerValue];
            
            if (result == 1) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UserInfoManager shareManager]loginWithUserDict:jsonDict[@"data"]];
                    [[CK_MenuViewController getMenuViewController].imgView sd_setImageWithURL:[NSURL URLWithString:jsonDict[@"data"][@"icon"]]];
                    [[CK_MenuViewController getMenuViewController].nameBtn setTitle:dataDic[@"uname"] forState:UIControlStateNormal];

                    [self dismissViewControllerAnimated:YES completion:nil];
                });
            }else{
                 UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleActionSheet];
                [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
                 dispatch_async(dispatch_get_main_queue(), ^{
                    [self presentViewController:alertC animated:YES completion:nil];
                });
            }
            
        }
    }];
    
    [dataTask resume];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
